package com.yc.web.controller;

import com.yc.DTO.DiaryDTO;
import com.yc.services.DiaryService;
import com.yc.web.controller.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/diary")
public class DiaryController {

    @Autowired
    private DiaryService diaryService;

    @PostMapping("/create")
    public ResponseResult createDiary(@RequestBody DiaryDTO diaryDTO) {
        return diaryService.createDiary(diaryDTO);
    }

    @PostMapping("/update")
    public ResponseResult updateDiary(@RequestBody DiaryDTO diaryDTO) {
        return diaryService.updateDiary(diaryDTO);
    }

    @PostMapping("/delete/{diaryId}")
    public ResponseResult deleteDiary(@PathVariable("diaryId") Long diaryId, @RequestParam("userId") Long userId) {
        return diaryService.deleteDiary(diaryId, userId);
    }

    @GetMapping("/list")
    public ResponseResult getDiaryList(
            @RequestParam("userId") Long userId,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return diaryService.getDiaryList(userId, pageNum, pageSize);
    }

    @GetMapping("/detail/{diaryId}")
    public ResponseResult getDiaryDetail(@PathVariable("diaryId") Long diaryId, @RequestParam("userId") Long userId) {
        return diaryService.getDiaryDetail(diaryId, userId);
    }

    // 在 DiaryController 类中添加以下方法
    @GetMapping("/search")
    public ResponseResult searchDiaryByTitle(
            @RequestParam("userId") Long userId,
            @RequestParam("title") String title,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return diaryService.searchDiaryByTitle(userId, title, pageNum, pageSize);
    }
}